package com.hankol.newretail.adapter.order

import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.core.view.isGone
import androidx.recyclerview.widget.RecyclerView
import com.baselibrary.Constants
import com.baselibrary.extension.checkValue
import com.baselibrary.extension.load
import com.baselibrary.extension.setMallPriceTTF
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.*
import com.baselibrary.view.RoundCornerImageView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.hankol.newretail.R
import com.hankol.newretail.bean.MallOrderSkuDtoBean
import com.hankol.newretail.bean.MyOrderListBean
import com.baselibrary.utils.SpecUtils

/**
 * @author: 关忆北、
 * @date: 2022/8/8
 * @Des：
 */
class MyOrderListAdapter : BaseQuickAdapter<MyOrderListBean, BaseViewHolder>(R.layout.item_order_list) {

    override fun convert(helper: BaseViewHolder, item: MyOrderListBean) {
        helper.setText(R.id.tv_order_store_name, item.shopName.checkValue())
        helper.setText(R.id.tv_order_state, OptionUtils.getOrderStatusString(item.status)).addOnClickListener(R.id.rl_order_multiple)
        val llOrderMultiple = helper.getView<RelativeLayout>(R.id.rl_order_multiple)
        val llOrderSingle = helper.getView<LinearLayout>(R.id.ll_order_single)
        if (item.orderListSkuDtoList.size > 1) {
            llOrderMultiple.visibility = View.VISIBLE
            llOrderSingle.visibility = View.GONE
            val rvOrderCover = helper.getView<RecyclerView>(R.id.rv_order_cover)
            val orderCoverListAdapter = OrderCoverListAdapter()
            rvOrderCover.adapter = orderCoverListAdapter
            orderCoverListAdapter.setNewData(item.orderListSkuDtoList)
            orderCoverListAdapter.setOnItemClickListener { adapter, view, position ->
                //进入订单详情
                val hashMap = HashMap<String, Any>()
                hashMap[Constants.KEY_ORDER_ID] = item.poId
                ARouterUtils.jump(ActivityRouter.NEW_RETAIL.ORDER_DETAIL, hashMap)
            }
            helper.setText(R.id.tv_order_sum_price, "${AmountUtil.changeF2YKeepTwo("${item.price}")}")
            helper.getView<TextView>(R.id.tv_order_sum_price).setMallPriceTTF(mContext)
            helper.setText(R.id.tv_order_sum_quantity, "共${item.num}件")
        } else if (item.orderListSkuDtoList.size == 1) {
            llOrderMultiple.visibility = View.GONE
            llOrderSingle.visibility = View.VISIBLE
            val product = item.orderListSkuDtoList[0]
            GlideUtils.loadImg_228(mContext, product.skuCover.checkValue(), helper.getView(R.id.round_order_cover))
            helper.setText(R.id.tv_order_title, product.spuName.checkValue())
            helper.setText(R.id.tv_order_norms, "${SpecUtils.getNormsStr(product.specList)}")
            helper.setText(R.id.tv_order_price, "${AmountUtil.changeF2YKeepTwo("${item.price}")}")
            helper.setText(R.id.tv_order_quantity, "数量：${product.num}")
            helper.getView<TextView>(R.id.tv_order_price).setMallPriceTTF(mContext)
            helper.getView<TextView>(R.id.tv_order_price_yuan).setMallPriceTTF(mContext)
            helper.setGone(R.id.iv_gift_label,product.giftType == 1)
        }
        helper.addOnClickListener(R.id.tv_order_cancel)
        helper.addOnClickListener(R.id.tv_order_pay)
        helper.addOnClickListener(R.id.rl_order_list_item)
        helper.addOnClickListener(R.id.ll_price_num)
        helper.addOnClickListener(R.id.tv_order_store_name)
        //状态：0-待付款（创建），1-待发货（已付款），2-待收货（已发货），3-待评价（已收货），5-已完成，6-已关闭
        when (item.status) {
            0 -> {
                helper.setGone(R.id.rl_bot_button, true)
                helper.setGone(R.id.tv_order_pay, true)
                helper.setGone(R.id.tv_order_cancel, false)
                helper.setText(R.id.tv_order_pay, "付款")
            }
            1 -> {
                helper.setGone(R.id.rl_bot_button, false)
                helper.setGone(R.id.tv_order_pay, false)
                helper.setGone(R.id.tv_order_cancel, false)
            }
            2 -> {
                helper.setGone(R.id.rl_bot_button, true)
                helper.setGone(R.id.tv_order_pay, true)
                helper.setGone(R.id.tv_order_cancel, false)
                helper.setText(R.id.tv_order_pay, "确认收货")
            }
            5 -> {
                helper.setGone(R.id.tv_order_pay, false)
                helper.setGone(R.id.tv_order_cancel, false)
                helper.setGone(R.id.rl_bot_button, false)
            }
            6 -> {
                helper.setGone(R.id.rl_bot_button, true)
                helper.setGone(R.id.tv_order_pay, true)
                helper.setGone(R.id.tv_order_cancel, false)
                helper.setText(R.id.tv_order_pay, "删除")
            }
        }
    }

    class OrderCoverListAdapter : BaseQuickAdapter<MallOrderSkuDtoBean, BaseViewHolder>(R.layout.item_order_cover_list) {
        override fun convert(helper: BaseViewHolder, item: MallOrderSkuDtoBean) {
            helper.setGone(R.id.iv_gift_label, item.giftType == 1)
            helper.getView<RoundCornerImageView>(R.id.cover).load(mContext, item.skuCover.checkValue())
            if (itemCount - 1 == helper.adapterPosition) {
                helper.setGone(R.id.iv_cover_station, true)
            } else {
                helper.setGone(R.id.iv_cover_station, false)
            }
        }

    }

}

